package com.example.ktapplication.net.interceptor

import com.elvishew.xlog.XLog
import okhttp3.Interceptor
import okhttp3.Request
import okhttp3.Response
import okio.Buffer

class RequestInterceptor: Interceptor {
    override fun intercept(chain: Interceptor.Chain): Response {
        val request = chain.request()
        // 打印请求信息
        val logBody = true
        // 或者根据需要决定是否打印请求体
        val requestCopy = request.newBuilder().build()
        val buffer = Buffer()
        if (logBody) {
            if (requestCopy.body != null) {
                requestCopy.body!!.writeTo(buffer)
            } else {
                buffer.writeUtf8("empty requestBody")
            }
        }

        val requestLog2="请求参数：\n${request.method}\n${request.url}\nbody:${buffer.readUtf8()}".trimIndent()
        XLog.d(requestLog2)

        return chain.proceed(request)
    }
}